home *** CD-ROM | disk | FTP | other *** search
-
- Turbo Pascal 5.0
- Heap3 Documentation
-
-
- INTRODUCTION
-
- In response to a user's request, here's a Turbo Pascal 5.0 (TP5)
- unit that implements a heap manager similar to Turbo Pascal 3.0
- (TP3). The main differences between this heap manager (Heap3) and
- the one built into TP5's System unit are:
-
- 1. Heap3 always allocates an even multiple of 8 bytes (TP5
- allocates exactly the number requested).
- 2. Heap3 stores its free list in address order as a linked
- list that runs throughout the heap (TP5 stores its free
- list at the top of the heap and grows downwards.)
- 3. Heap3 supplies only GetMem, FreeMem, MemAvail and MaxAvail
- (TP5 also supports New, Dispose, Mark and Release).
-
- For more information about these differences, refer to the notes
- at the end of this documentation.
-
-
-
- HEAP3 INTERFACE DECLARATIONS
-
- Here's the TP5 interface for unit Heap3:
-
- unit Heap3;
-
- interface
-
- procedure GetMem(var P; Size: Word);
- procedure FreeMem(var P; Size: Word);
-
- function MemAvail: LongInt;
- function MaxAvail: LongInt;
-
-
-
- TECHNICAL NOTES:
-
- 1. You should make Heap3 the first unit used in a USES clause.
- In addition, rebuild all units that use System's heap
- routines. For example, if you are going to use Heap3 in a
- program named TEST.PAS, you should completely rebuild all of
- the units used by TEST. And be sure to place a USES HEAP3 in
- every unit that calls any TP5 heap management routines
- (GetMem, FreeMem, New, Dispose, Mark, Release, MaxAvail,
- MemAvail). In this way, all references to heap routines in
- these other units will now link in Heap3's routines (instead
- of System's).
-
- Of course, this requirement could present an insurmountable
- problem if you don't have the source to SOMEUNIT.TPU and it
- uses the heap. In such a case, you can't use Heap3 in the
- same program as SOMEUNIT unless SOMEUNIT has some provision
- for intercepting heap allocation and deallocation requests.
- TP5's Graph unit is such an example; though it uses the heap
- to allocate and deallocate memory, it has two "hooks"
- (GraphGetMemPtr and GraphFreeMemPtr) that allow a user
- program to insert its own heap routines. Thus, you CAN use
- Heap3 and Graph in the same program, but you should place
- Heap3 first in the USES clause (so its initialization code is
- executed first) and you MUST point GraphGetMemPtr and
- GraphFreeMemPtr at your own routines (see TP5 Reference
- Manual for details).
-
- 2. Do not make calls to New, Dispose, Mark or Release in any
- program that uses the Heap3 unit. These routines are not
- supported by Heap3. Since Heap3 uses the data structures that
- are declared in the System unit, mixing calls to heap
- routines from System and Heap3 will have unpredictable (and
- probably catastrophic) consequences. To help prevent these
- types of errors, Heap3 contains "dummy" procedure
- declarations for New, Dispose, Mark and Release. When any of
- these "dummy" routines is referenced in a program that uses
- Heap3, error #85 (semi-colon expected) will be generated.
-
- 3. To use Heap3, all calls to
-
- New(x);
- Dispose(x);
-
- will have to be changed to
-
- GetMem(x, SizeOf(x^));
- FreeMem(x, SizeOf(x^));
-
- Of course, you can hardcode the SizeOf() argument and thus
- the following procedure calls would be valid:
-
- GetMem(x, 12);
- FreeMem(x, 36);
-
- The parameter to Heap3's GetMem and FreeMem is an untyped var
- parameter. This means you don't have to typecast every
- pointer you pass to GetMem and FreeMem, but it also means you
- can pass a variable of any type (by accident).
-
- 4. With version 3.0 of Turbo Pascal, a runtime error ($FF
- Heap/Stack collision) occurred when insufficient dynamic
- memory was available to satisfy a call to New or GetMem.
- TP5's heap manager supports a heap error handler called
- HeapError (see the "Inside Turbo Pascal" chapter for more
- information about HeapError).
-
- Similarly, instead of generating a runtime error like TP3,
- Heap3's GetMem returns a nil pointer when the heap is full.
- Therefore, after each call to GetMem(P, SomeBytes), your
- program can (and should) verify that the allocation request
- succeeded by checking P <> nil.
-
- Also, unlike the TP5 heap manager built into the System unit,
- Heap3 does NOT give an error if P does not point into the
- heap and a FreeMem(P, SomeBytes) is called.
-
- 5. The smallest heap allocation using Heap3 is 8 bytes, and
- allocation requests will always be rounded to an even
- multiple of 8 bytes.